// pages/valuation/valuation.js
var app = getApp()
var modal = require('../../utils/modal');
var request = require('../../utils/request');
var utils = require('../../utils/utils');
var storage = require('../../utils/storage');
var pageSetting = {
    data:{
        valuation:{
            commentVal:'',
            isDisabled:false,
            focus:true
        },
        actId: '',
        orderId: '',
        actName: ''
    },
    onLoad: function(options) {
        var actId = options.act_id - 0;
        var orderId = options.order_id - 0;
        var actName = options.act_name;
        this.setData({
            actId: actId,
            orderId: orderId,
            actName: actName
        })
    },
    inputChange:function(e){
        // 监听输入框文字变化，改变发送按钮状态
        var that = this;
        var val = e.detail.value;
        if (val.length > 0 && val != ''){
            that.setData({
                'valuation.isDisabled':true
            });
        }else {
             that.setData({
                'valuation.isDisabled':false
            });
        }
    },
    formSubmit:function(e){
        // 点击发送按钮，触发提交事件
        var val = e.detail.value.textarea,
        that = this;
        if(val.length >= 5) {
            var data = {
                'dp:api:act:ordercomment':{
                    'act_id':that.data.actId,
                    'order_id':that.data.orderId,
                    'content':val
                }
            }
            request.post({
            path: '/wxapp_api/api'
        },data)
        .then(function(res){
            var retData = res['dp:api:act:ordercomment'];
            if(retData.ret != 0){
                modal.toast(retData.msg);
                // wx.navigateTo({
                //     url: "../error/error"
                // })
            }else{
                var commentId = retData.data.commentId;
                var articleId = retData.data.articleId;
                var actId = that.data.actId;
                var actName = that.data.actName;
                app.globalData.listOrderState = true;
                app.globalData.orderDetailComment = true;
                wx.showModal({
                    title: '评价成功',
                    content: '感谢您的精彩评价！\r\n请收下5积分的小小奖励,赞！',
                    confirmText: '去看看',
                    cancelText: '知道了',
                    success: function(res) {
                        if (res.confirm) {
                            wx.redirectTo({
                                url: "../valuation-detail/valuation-detail?articleId=" + articleId + "&commentId=" + commentId + "&id=" + articleId + "&act_id=" + actId + "&actName=" + actName + "&comeFrom=dp_order"
                            })
                        } else if (res.cancel) {
                            wx.navigateBack({
                                delta: 1
                            })
                        }
                    }
                })
                that.setData({
                    'valuation.commentVal':'',
                    'valuation.isDisabled':false,
                    'commentId': commentId
                });    
            }
        });
                
        } else {
            modal.toast("评价至少5个字哦~");
        }
    },
    backPrevPage:function(){
        wx.navigateBack({
            delta: 1
        })
    }
};
utils.assign(pageSetting);
Page(pageSetting);